﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication6
{
    public partial class gouwuche : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //把商品加入到购物车
            
            //首先判断该用户是否登录
            if (Session["yonghu"]==null)
            {
                Response.Write("<script language=javascript>alert('请先登录');window.location.href='login.aspx';</script>");
                return;
            }
            string username = Convert.ToString(Session["yonghu"]);    

            //获取商品信息            
            string spid = Request["spid"];

            string sql = " select * from t_product where id= " + spid; 
            DataTable dt = SQLHelper.ExecuteDataTable(sql);

            string spname = "";
            string jiage = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                spname = Convert.ToString(row["spname"]);
                jiage = Convert.ToString(row["jiage"]);
            }

            //判断该商品在购物车里是否存在

            sql = " select count(1) from t_gouwuche where username= '" + username + "' and spid= " + spid;
            int count = Convert.ToInt32(SQLHelper.ExecuteScalar(sql));
            if (count>0)
            {
                Response.Write("<script language=javascript>alert('该商品在购物车里已经存在，请勿重复添加');window.location.href='index.aspx';</script>");
                return;
            }

            //把商品加入购物车
            sql = @" insert into t_gouwuche (username,spname,num,spid,jiage,zongjia) 
            values('" + username + "','" + spname + "'," + 1 + "," + spid + "," + jiage + "," + jiage + ")";

            SQLHelper.ExecuteNonQuery(sql);
            Response.Write("<script language=javascript>alert('添加成功');window.location.href='gouwuchelist.aspx';</script>");
            return;


        }
    }
}
